package com.example.l2048;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import org.w3c.dom.Text;

import java.util.Vector;

public class GameCanvas extends FrameLayout {
    //public:
    //构造函数
    public GameCanvas(Context context, int _gameCanvasWidth,
                      TextView _tvSocre, TextView _tvMaxScore, TextView _reStart,
                      SharedPreferences _sp){
        super(context);

        //设置画布样式
        SetGameCanvasState(_gameCanvasWidth);
        //获取父级控件 & 设置控件菜单
        this.tvCurScore = _tvSocre;
        this.tvMaxScore = _tvMaxScore;
        this.reStart = _reStart;
        this.sp = _sp;
        this.SetGameMenu();
        //创建监听
        SetTouchEvent();
        //设置卡片样式
        SetCardGrid();
        //设置状态类
        gameAnimation = new AnimatorController(this.getContext(),this, cardWidth, dxWidth);
        //开始游戏
        this.GameStart();
    }

    //设置样式以及触摸事件
    public void SetGameCanvasState(int _gameCanvasWidth){
        //设置画布宽度
        gameCanvasWidth = _gameCanvasWidth;
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(this.gameCanvasWidth, this.gameCanvasWidth);

        //设置居中
        params.gravity = Gravity.CENTER;
        //应用设置
        this.setLayoutParams(params);

        //设置圆角
        Drawable flGameCanvasDrawable= ContextCompat.getDrawable(this.getContext(), R.drawable.fl_game_canvas);
        this.setBackground(flGameCanvasDrawable);

        //设置卡片宽度
        this.cardWidth = (int)(this.gameCanvasWidth * 0.9 / 4);
        this.dxWidth = (int)(this.gameCanvasWidth * 0.1 / 5);
    };
    public void SetGameMenu(){
        //<<-----TextView tvGameEnd------>>
        //设置大小
        tvGameEnd = new TextView(this.getContext());
        FrameLayout.LayoutParams tvGameEndParams = new FrameLayout.LayoutParams(this.gameCanvasWidth ,this.gameCanvasWidth);
        tvGameEnd.setLayoutParams(tvGameEndParams);
        //设置圆角大小
        GradientDrawable  tvGameEndDrawable = new GradientDrawable();
        tvGameEndDrawable.setColor(Color.BLACK);                    //设置默认颜色
        tvGameEndDrawable.setCornerRadius(15);                      //设置圆角
        tvGameEnd.setBackground(tvGameEndDrawable);                 //应用设置
        //设置文字样式
        tvGameEnd.getBackground().setAlpha(50);
        tvGameEnd.setTextSize(80);
        tvGameEnd.setGravity(Gravity.TOP | Gravity.CENTER);
        tvGameEnd.setTextColor(Color.WHITE);
        tvGameEnd.setText("重新开始");

        //<<-----TextView Buttom1------>>
        //设置大小
        tvButtom1 = new TextView(this.getContext());
        FrameLayout.LayoutParams tvButtom1Params = new FrameLayout.LayoutParams(this.gameCanvasWidth/2 ,this.gameCanvasWidth/2);
        tvButtom1Params.setMargins(0,this.gameCanvasWidth/2,0,0);
        tvButtom1.setLayoutParams(tvButtom1Params);
        //应用设置
        tvButtom1.setBackground(tvGameEndDrawable);
        //设置文字属性
        tvButtom1.getBackground().setAlpha(15);
        tvButtom1.setGravity(Gravity.CENTER);
        tvButtom1.setTextSize(80);
        tvButtom1.setTextColor(Color.WHITE);
        tvButtom1.setText("是");

        //<<-----TextView Buttom2------>>
        //设置大小
        tvButtom2 = new TextView(this.getContext());
        FrameLayout.LayoutParams tvButtom2Params = new FrameLayout.LayoutParams(this.gameCanvasWidth/2 ,this.gameCanvasWidth/2);
        tvButtom2Params.setMargins(this.gameCanvasWidth/2,this.gameCanvasWidth/2,0,0);
        tvButtom2.setLayoutParams(tvButtom2Params);
        //应用设置
        tvButtom2.setBackground(tvGameEndDrawable);
        //设置文字属性
        tvButtom2.getBackground().setAlpha(15);
        tvButtom2.setGravity(Gravity.CENTER);
        tvButtom2.setTextSize(80);
        tvButtom2.setTextColor(Color.WHITE);
        tvButtom2.setText("否");

        tvButtom1.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event){
                if(event.getAction() == MotionEvent.ACTION_UP){
                    removeView(tvButtom1);
                    removeView(tvButtom2);
                    removeView(tvGameEnd);
                    GameStart();
                }
                return true;
            }
        });

        tvButtom2.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event){
                if(event.getAction() == MotionEvent.ACTION_UP){
                    removeView(tvButtom1);
                    removeView(tvButtom2);
                    removeView(tvGameEnd);
                    setOnTouchListener(moveTouch);
                }
                return true;
            }
        });
    }
    public void SetCardGrid(){
        this.CardGrid = new Card[4][4];
        for(int x = 0; x < 4; ++x){
            for(int y = 0; y < 4; ++y){
                CardGrid[x][y] = new Card(this.getContext(), cardWidth,
                        dxWidth * (x+1) + cardWidth * x,
                        dxWidth * (y+1) + cardWidth * y);
                addView(CardGrid[x][y]);
            }
        }
    }
    public void SetTouchEvent(){
        moveTouch = new View.OnTouchListener() {
            private float x,y,delatX,delatY;
            //重写触摸事件
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        x = event.getX();
                        y = event.getY();

                        break;
                    case MotionEvent.ACTION_UP:
                        delatX = event.getX() - x;
                        delatY = event.getY() - y;

                        if (Math.abs(delatX) > Math.abs(delatY) && delatX < -5) {
                            MoveLeft();
                        }
                        else if(Math.abs(delatX) > Math.abs(delatY) && delatX > 5) {
                            MoveRight();
                        }
                        else if(Math.abs(delatX) < Math.abs(delatY) && delatY < -5) {
                            MoveUp();
                        }
                        else if(Math.abs(delatX) < Math.abs(delatY) && delatY > 5) {
                            MoveDown();
                        }
                        break;
                }
                return true;
            }
        };
        reStartTouch = new View.OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_UP){
                    GameEnd();
                }
                return true;
            }
        };
        setOnTouchListener(moveTouch);
        reStart.setOnTouchListener(reStartTouch);
    }

    //开始游戏
    public void GameStart(){
        //初始化卡片网格和状态网格
        gameAnimation.ClearState();
        //添加卡片
        gameAnimation.AddCard();
        gameAnimation.AddCard();

        //设置当前分数
        this.curScore = 0;
        this.tvCurScore.setText(String.valueOf(this.curScore));

        //设置最大分数
        this.maxScore = sp.getInt("max",0);
        this.tvMaxScore.setText(String.valueOf(maxScore));

        //设置触控事件
        setOnTouchListener(moveTouch);
    }
    //游戏结束
    public void GameEnd(){
        setOnTouchListener(null);
        this.addView(tvGameEnd);
        this.addView(tvButtom1);
        this.addView(tvButtom2);
    }

    public void ClearCardGrid(){
        for(int x = 0; x < 4; ++x){
            for(int y = 0; y < 4; ++y){
                if(gameAnimation.StateGird[x][y] == 0)
                CardGrid[x][y].SetCardNumber(0);
            }
        }
    }

    //增加分数
    public void UpdateScore(int _newScore){
        this.curScore = this.curScore + _newScore;
        this.tvCurScore.setText(String.valueOf(this.curScore));

        if(curScore > maxScore){
            maxScore = curScore;
            SharedPreferences.Editor edit = sp.edit();
            edit.putInt("max", maxScore);
            edit.apply();
            boolean commit = edit.commit();
            tvMaxScore.setText(String.valueOf(maxScore));
        }
    }

    //移动事件
    public void MoveUp(){
        gameAnimation.MoveUp();
    }
    public void MoveDown(){
        gameAnimation.MoveDown();
    }
    public void MoveLeft(){
        gameAnimation.MoveLeft();
    }
    public void MoveRight(){
        gameAnimation.MoveRight();
    }

    //同步状态
    public void UpdateState(int[][] _StateGird){
        for( int y = 0; y < 4; ++y ){
            for( int x = 0; x < 4; ++x){
                this.CardGrid[x][y].SetCardNumber(_StateGird[x][y]);
            }
        }
    }

    //private:
    private int gameCanvasWidth;        //画布宽度
    private int cardWidth;              //卡片宽度
    private int dxWidth;                //间隔宽度
    private Card[][] CardGrid;          //卡片网格

    //功能TextView
    private TextView tvCurScore;
    private int curScore;
    private TextView tvMaxScore;
    private int maxScore;
    private TextView reStart;

    private TextView tvGameEnd;
    private TextView tvButtom1;
    private TextView tvButtom2;
    private SharedPreferences sp;

    //监听事件
    View.OnTouchListener moveTouch;
    OnTouchListener reStartTouch;
    //动画状态类
    AnimatorController gameAnimation;
}
